//
//  DetailViewController.m
//  SeplitDemo
//
//  Created by 信恩世通 on 2017/8/1.
//  Copyright © 2017年 Xiyuekeji. All rights reserved.
//

// controller
#import "DetailViewController.h"
// view
#import "SelectedGoodsCell.h"
#import "NullView.h"
//model
#import "GoodsModel.h"

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataSource;

@end

@implementation DetailViewController

static NSString * const SelectedGoodsCellID = @"SelectedGoodsCell";

#pragma mark - **** lazy loading ****

static NSString * const dataSourceData = @"dataSourceData";

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self commonInit];
    [self setupNavigationBar];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (0 == self.dataSource.count && self.tableView.tableFooterView == nil) {
        self.tableView.tableFooterView = [[NullView alloc] initWithFrame:CGRectMake(0, 0, self.view.width - 1, self.view.height)];
    } else {
        self.tableView.tableFooterView = nil;
    }
}

#pragma mark - **** parivate method ****

/** 设置UI */
- (void)commonInit {
    
    CALayer *leftBorder = [[CALayer alloc] init];
    leftBorder.borderWidth = 1;
    leftBorder.borderColor = COLOR_BORDER_BG.CGColor;
    leftBorder.frame = CGRectMake(0, 0, 1, self.view.height);
    [self.view.layer addSublayer:leftBorder];
    
    self.view.backgroundColor = COLOR_BG;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tableView.backgroundColor = COLOR_BG;
    tableView.showsVerticalScrollIndicator = YES;
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.separatorColor = COLOR_BORDER_BG;
    tableView.estimatedRowHeight = 130;
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[SelectedGoodsCell class] forCellReuseIdentifier:SelectedGoodsCellID];
    [self.view  addSubview:tableView];
    self.tableView = tableView;
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 1, 0, 0));
    }];
}

/** 设置导航栏 */
- (void)setupNavigationBar {
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [rightBtn setTitle:@"清空" forState:UIControlStateNormal];
    [rightBtn setTitleColor:COLOR_GRAY forState:UIControlStateDisabled];
    [rightBtn setTitleColor:COLOR_TEXT_HIGHT forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(clearDataSource:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.frame = CGRectMake(-SPACE, 5, 50, 30);
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -SPACE;
    
    self.navigationItem.rightBarButtonItems = @[spaceItem,rightItem];
    self.navigationItem.rightBarButtonItems.lastObject.enabled = NO;
    
}

- (void)clearDataSource:(UIButton *)sender {
    [self.dataSource removeAllObjects];
    
    [self refreshUIAndBottomCallBack];
}

- (void)refreshUIAndBottomCallBack {
    [self.tableView reloadData];
    // 修改底部数据及回调
    //...
    // 检查缺省视图
    if (0 == self.dataSource.count && self.tableView.tableFooterView == nil) {
        self.tableView.tableFooterView = [[NullView alloc] initWithFrame:CGRectMake(0, 0, self.view.width - 1, self.view.height)];
    } else {
        self.tableView.tableFooterView = nil;
    }
    
    self.navigationItem.rightBarButtonItems.lastObject.enabled = self.dataSource.count;
}


#pragma mark - **** AddGoodsProtocol ****

-(void)addSelectedGoodsDic:(NSDictionary *)goodsDic {
    
    for(GoodsModel* goods in self.dataSource){
        if ([goods.ProductId isEqualToString:goodsDic[@"ProductId"]]) {
            goods.buyCount++;
            [self refreshUIAndBottomCallBack];
            return;
        }
    }
    
    GoodsModel* newGoods = [GoodsModel mj_objectWithKeyValues:goodsDic];
    [self.dataSource addObject:newGoods];
    
    [self refreshUIAndBottomCallBack];
    
}


#pragma mark - **** UITableViewDataSource ****

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectedGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:SelectedGoodsCellID];
    cell.goodsItem = self.dataSource[indexPath.row];
    
    return cell;
}

#pragma mark - **** UITableViewDelegate ****

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 150;
//}


@end
